//
//  DZDVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/27.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
 
class ZyaDZDVC: UITableViewController,DateWidgetable {
   
    var pmonth = Date.preMonths(1)
   private var main = ZyaDZDModel(JSON:["":""])!
    
    lazy var header:ZyaDZDHeader = {
       let v = ZyaDZDHeader()
       v.frame = CGRect(x:0,y:0,width:KSGlobal.screenW,height:140)
       return v
   }()
   
   lazy var descLab:UILabel = {
       let v = UILabel()
       v.backgroundColor = .white
       v.textAlignment = .center
       v.frame = CGRect(x:0,y:0,width:KSGlobal.screenW,height:38)
       return v
   }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self .navigationController?.navigationBar.setBackgroundImage(UIImage.from(color: Theme.primaryColor()), for: .any, barMetrics: .default)
        self .navigationController?.navigationBar.shadowImage = UIImage()
        self .navigationController?.navigationBar.isTranslucent = false
    }
   override func viewDidLoad() {
       super.viewDidLoad()

       self.title = "对账单"
    
       let tap = UITapGestureRecognizer(target: self, action: #selector(go2statistic))
       header.addGestureRecognizer(tap)
       header.btn1.setTitle("\(self.pmonth)", for: .normal)
       self.tableView.tableHeaderView = header
        self.tableView.register(ZyaDZDCell.self, forCellReuseIdentifier: ZyaDZDCell.identifier)
       
         self.tableView.rowHeight = 130
       self.tableView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.9607843137, alpha: 1)
       self.tableView.separatorStyle = .none

       self.tableView.tableFooterView = UIView()
       self.tableView.mj_header = KSRefreshHeader {
            self.network()
       }
    
       self.tableView.mj_header?.beginRefreshing()
       
       
   }


   
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       tableView.deselectRow(at: indexPath, animated: true)
        let vc = ZyaDZDDetailVC()
       if let det = self.main.homePageDetail {
           vc.type = det[indexPath.row].type.unwrap
           vc.id_key = det[indexPath.row].idKey.unwrap
       }
         self.navigationController?.pushViewController(vc, animated: true)
       
   }
   
   
   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       if let arr = self.main.homePageDetail {
           return arr.count
        } else {
           return 0
        }
   }
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaDZDCell.identifier, for: indexPath) as? ZyaDZDCell else {
           fatalError()
       }
       if let arr = self.main.homePageDetail {
           cell.one = arr[indexPath.row]
       }
       return cell
       
   }
  
   
  @objc func go2statistic() {
      
      let view = KSSelectMonthView()
      view.fetchData = { month in
          self.pmonth = month
          self.header.btn1.setTitle("\(self.pmonth)", for: .normal)
          self.tableView.mj_header?.beginRefreshing()
      }
      view.show()
      
      
 
   }
 
   func network() {
       KSNetwork.shared.doRequest(API.supplierCheckBillHome(p_supplier_id: USERINFO.providerId.unwrap, p_month: pmonth),showHud: false) { (data:BaseResult<ZyaDZDModel>) in
           if data.flag == TRUE {
               if let info = data.info_object {
                   self.main = info
                   self.header.btn2.setTitle(self.main.monthSockMoney.unwrap.zero4empty, for: .normal)
                   self.header.btn4.setTitle(self.main.monthYfMoney.unwrap.zero4empty, for: .normal)
                   self.header.btn6.setTitle(self.main.monthDfMoney.unwrap.zero4empty, for: .normal)

                   self.tableView.reloadData()
               }
           } else {
               KSToast(data.errorCombine.unwrap)
           }
           self.tableView.mj_header?.endRefreshing()
       }

   }
   
   
   
   
}
class ZyaDZDHeader:UIView {
    lazy var topBg:UIView = {
       let v = UIView()
        v.backgroundColor = Theme.primaryColor()
         return v
    }()
    lazy var bottomBg:UIView = {
       let v = UIView()
        v.backgroundColor = Theme.awModuleBgColor
         return v
    }()
    lazy var bg:UIView = {
       let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
         return v
    }()
   class LLLButton: UIButton {
       
       override func layoutSubviews() {
           super.layoutSubviews()
           titleLabel?.frame = CGRect(x: 0 , y: 0, width: self.frame.width, height: self.frame.height)
        }

   }
   lazy var btn1:LLLButton = {
      let v = LLLButton()
        v.setTitleColor(.darkGray, for: .normal)
        return v
   }()
   lazy var arrow:UIButton = {
      let v = UIButton()
       v.setImage(UIImage(named:"cellclose_gray"), for: .normal)
        return v
   }()
   lazy var btn2:UIButton = {
      let v = UIButton()
       v.setTitle("--", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)

       return v
   }()
   lazy var btn3:UIButton = {
       let v = UIButton()
       v.setTitle("应付(元)", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
   lazy var btn4:UIButton = {
       let v = UIButton()
       v.setTitle("--", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)

        return v
    }()
   lazy var btn5:UIButton = {
       let v = UIButton()
       v.setTitle("已付(元)", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.systemFont(ofSize: 15)

        return v
    }()
   lazy var btn6:UIButton = {
       let v = UIButton()
       v.setTitle("--", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)

        return v
    }()
   lazy var btn7:UIButton = {
       let v = UIButton()
       v.setTitle("待付(元)", for: .normal)
       v.setTitleColor(.darkGray, for: .normal)
       v.titleLabel?.font = UIFont.systemFont(ofSize: 15)

        return v
    }()
   
   override init(frame: CGRect) {
       super.init(frame: frame)
       self.addSubview(topBg)
       self.addSubview(bottomBg)
       self.addSubview(bg)
       topBg.snp.makeConstraints { make in
           make.left.right.top.equalTo(self)
           make.height.equalTo(90)
       }
       bottomBg.snp.makeConstraints { make in
           make.left.right.bottom.equalTo(self)
           make.height.equalTo(50)
       }
       bg.snp.makeConstraints { make in
           make.left.equalTo(self).offset(6)
           make.right.equalTo(self).offset(-6)
           make.top.equalTo(self).offset(15)
            make.bottom.equalTo(self).offset(-15)
        }
       self.bg.addSubview(btn1)
       self.bg.addSubview(arrow)
       self.bg.addSubview(btn2)
       self.bg.addSubview(btn3)
       self.bg.addSubview(btn4)
       self.bg.addSubview(btn5)
       self.bg.addSubview(btn6)
       self.bg.addSubview(btn7)
       btn1.isUserInteractionEnabled = false
       btn2.isUserInteractionEnabled = false
       btn3.isUserInteractionEnabled = false
       btn4.isUserInteractionEnabled = false
       btn5.isUserInteractionEnabled = false
       btn6.isUserInteractionEnabled = false
       btn7.isUserInteractionEnabled = false
       arrow.isUserInteractionEnabled = false

       btn1.snp.makeConstraints { make in
           make.top.left.equalTo(bg).offset(5)
           make.right.equalTo(bg).offset(-35)
           make.height.equalTo(30)
       }
       arrow.snp.makeConstraints { make in
           make.centerY.equalTo(btn1)
           make.right.equalTo(bg).offset(-8)
           make.height.equalTo(18)
           make.width.equalTo(18)
       }
       btn2.snp.makeConstraints { make in
           make.top.equalTo(btn4)
           make.left.equalTo(bg).offset(5)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }
       btn3.snp.makeConstraints { make in
           make.top.equalTo(btn5)
           make.centerX.equalTo(btn2)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }
       btn4.snp.makeConstraints { make in
           make.top.equalTo(bg).offset(47)
           make.centerX.equalTo(bg)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }
       btn5.snp.makeConstraints { make in
           make.top.equalTo(btn4.snp.bottom)
           make.centerX.equalTo(bg)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }
       btn6.snp.makeConstraints { make in
           make.top.equalTo(btn4)
           make.right.equalTo(bg).offset(-5)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }
       btn7.snp.makeConstraints { make in
           make.top.equalTo(btn5)
           make.centerX.equalTo(btn6)
           make.height.equalTo(30)
           make.width.equalTo(120)
       }

       
   }
   
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}
 
